package com.lijie.bs_recruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lijie.bs_recruit.Util.WrapperUtil;
import com.lijie.bs_recruit.mapper.CompanyMapper;
import com.lijie.bs_recruit.mapper.HrMapper;
import com.lijie.bs_recruit.model.Company;
import com.lijie.bs_recruit.model.Hr;
import com.lijie.bs_recruit.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Classname BusinessServiceImpl
 * @Description TODO
 * @Date 2021/4/2 14:24
 * @Created by lijie
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {
    @Autowired
    CompanyMapper companyMapper;
    @Autowired
    HrMapper hrMapper;
    @Autowired
    WrapperUtil wrapperUtil;


    @Override
    public Boolean addCompany(Integer hrId, Company company) {
        UpdateWrapper<Hr> wrapper=new UpdateWrapper<>();
        wrapper.eq("hrId", hrId);

        if(company.getCompanyId()!=null){//前端传过来companyId，意味着只需要绑定,而不是新增
            wrapper.set("companyId", company.getCompanyId());
            wrapper.set("companyName",company.getCompanyName());
        }else{
            Integer integer = this.insertCompanyAuto(company);
            //将公司Id写入到hr中
            if(integer!=0){
                wrapper.set("companyId", integer);
                wrapper.set("companyName",company.getCompanyName());
            }
        }
        return hrMapper.update(new Hr(), wrapper)>0;
    }

    @Override
    public PageInfo<Company> selCompanyPage(Integer pageIndex, Company business) {
        QueryWrapper<Company> businessQueryWrapper = wrapperUtil.wrapperBusiness(business);
        PageHelper.startPage(pageIndex,8);
        PageInfo<Company> pageInfo = new PageInfo<>(companyMapper.selectList(businessQueryWrapper));
        return pageInfo;
    }

    @Override
    public Company selCompanyByhrId(Integer hrId) {
        Company company = companyMapper.selCompanyByhrId(hrId);
        return company;
    }

    @Override
    public Integer insertCompanyAuto(Company company) {
        baseMapper.insert(company);
        return company.getCompanyId();
    }

    @Override
    public List<Company> selCompany(Company company) {
        QueryWrapper<Company> wrapper=wrapperUtil.wrapperBusiness(company);
        List<Company> companies = baseMapper.selectList(wrapper);
        return companies;

    }
}
